﻿#region 命名空间
using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
#endregion

namespace ContentFactory
{
    /// <summary>
    /// 应用程序。
    /// </summary>
    public partial class App : Application
    {
        #region 字段

        int? _languageResourceIndex;

        #endregion

        #region 方法

        /// <summary>
        /// 设置应用程序的语言。
        /// </summary>
        public void ApplyLanguage(string language)
        {
            string languagesDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Languages", language);
            if (Directory.Exists(languagesDirectory))
            {
                bool hasResource = false;
                ResourceDictionary languageResource = new ResourceDictionary();
                DirectoryInfo languagesDirectoryInfo = new DirectoryInfo(languagesDirectory);
                FileInfo[] languageFileInfos = languagesDirectoryInfo.GetFiles();
                foreach (var fileInfo in languageFileInfos)
                {
                    if (fileInfo.Extension.ToUpper() == ".XAML")
                    {
                        using (Stream stream = new FileStream(fileInfo.FullName, FileMode.Open))
                        {
                            ResourceDictionary subResource = XamlReader.Load(stream) as ResourceDictionary;
                            if (subResource != null)
                            {
                                languageResource.MergedDictionaries.Add(subResource);
                                hasResource = true;
                            }
                        }
                    }
                }
                if (!hasResource) return;
                if (_languageResourceIndex.HasValue)
                {
                    Resources.MergedDictionaries[_languageResourceIndex.Value] = languageResource;
                }
                else
                {
                    Resources.MergedDictionaries.Add(languageResource);
                    _languageResourceIndex = Resources.MergedDictionaries.Count - 1;
                }
                string languagesConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Languages", "Language.config");
                using (Stream stream = new FileStream(languagesConfig, FileMode.OpenOrCreate))
                {
                    StreamWriter writer = new StreamWriter(stream);
                    writer.Write(language);
                }
            }
        }

        #endregion

        #region 事件处理

        // 应用程序启动时。
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            string languagesConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Languages", "Language.config");
            string language = null;
            using (Stream stream = new FileStream(languagesConfig, FileMode.Open))
            {
                StreamReader reader = new StreamReader(stream);
                language = reader.ReadToEnd();
                language.Trim();
            }
            this.ApplyLanguage(language);
        }

        #endregion
    }
}
